﻿using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.IO;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.AddressableAssets;
#endif

[System.Serializable]
public class PackageLabelData : ScriptableObjectDictionary<string,string>
{
    static PackageLabelData _Setting;
    /// <summary>是否是整包</summary>
    public static bool IsAllPacakage = true;

    public static string Path = "Assets/Editor/Address/PackageLabelData.asset";
    public static PackageLabelData Setting
    {
        get
        {
            if (_Setting == null)
            {
                string path = Path;
#if UNITY_EDITOR
                if (File.Exists(path))
                    _Setting = AssetDatabase.LoadAssetAtPath<PackageLabelData>(path);
                else
                {
                    _Setting = ScriptableObject.CreateInstance<PackageLabelData>();
                    AssetDatabase.CreateAsset(_Setting, path); 
                }
#endif
            }


            return _Setting;
        }
    }

    public void ClearTempData()
    {

    }

    public string GetLabel(string path)
    {
        string lab = null;
        //if (IsAllPacakage)
        //{
        //    return "default";
        //}
        
        while (!path.EndsWith("Assets"))
        {
            if (Target.TryGetValue(path, out lab))
                return lab;

            int index = path.LastIndexOf("/");
            path = path.Remove(index);
        }

        if (lab == null)
        {
            lab = "hot";
        }


        return lab;
    }

    public string GetAddressLabel(string key)
    {
        string lab = GetLabel(key);

#if UNITY_EDITOR
        if (lab != null)
        {
            var m_Settings = AddressableAssetSettingsDefaultObject.Settings;
            if (!m_Settings.GetLabels().Contains(lab))
                m_Settings.AddLabel(lab);
        }
#endif
        return lab;
    }

    [SerializeField]
    /// <summary>锁定列表</summary>
    public List<string> LockList;
    Dictionary<string, bool> mLockList;

    public bool HaveLock(string key)
    {
        if (LockList == null) return false;
        if (mLockList == null) {

            mLockList = new Dictionary<string, bool>();
            int cnt = LockList.Count-1;
            for(;cnt>=0;cnt--)
            {
                mLockList.Add(LockList[cnt], true);
            }
        }

        bool tmpB;
        return mLockList.TryGetValue(key,out tmpB);
    }

    public void AddLock(string key)
    {
        if(null != mLockList)
        {
            bool tmpB;
           if(!mLockList.TryGetValue(key, out tmpB))
           {
                mLockList.Add(key, true);
           }
        }

        if (LockList == null)
            LockList = new List<string>();

        if (LockList.IndexOf(key) == -1)
            LockList.Add(key);
    }

    public void RemoveLock(string key)
    {
        if (null != mLockList)
        {
            bool tmpB;
            if (mLockList.TryGetValue(key, out tmpB))
            {
                mLockList.Remove(key);
            }
        }

        if(LockList.IndexOf(key) != -1)
            LockList.Remove(key);
    }

    public void Save()
    {
        if (keys == null) return;

        string[] lines = File.ReadAllLines(Path);
        List<string> saveLines = new List<string>();

        int cnt = lines.Length;
        for(int i = 0; i < cnt;i++)
        {
            if(lines[i].IndexOf("keys:") != -1) break;

            saveLines.Add(lines[i]);
        }

        saveLines.Add("  keys:");
        cnt = keys.Count;
        for (int i = 0; i < cnt; i++)
            saveLines.Add(GStringBuilder.Contact("  - ", keys[i]));

        saveLines.Add("  values:");
        cnt = values.Count;
        for (int i = 0; i < cnt; i++)
            saveLines.Add(GStringBuilder.Contact("  - ", values[i]));

        saveLines.Add("  LockList:");
        if (LockList == null) return;

        cnt = LockList.Count;
        for (int i = 0; i < cnt; i++)
            saveLines.Add(GStringBuilder.Contact("  - ", LockList[i]));

        File.WriteAllLines(Path,saveLines.ToArray());
    }
}
